#include "stdafx.h"
#include "win_cs.h"
#include "win_function_wrapper.h"

_SSL_BEGIN
_SSWINOSL_BEGIN

cs_lock::cs_lock()
{
	if(wrapper_.InitializeCriticalSectionEx != NULL)
		wrapper_.InitializeCriticalSectionEx(&cs_, 5000, 0);
	else
	::InitializeCriticalSection(&cs_);
}

cs_lock::~cs_lock()
{
	::DeleteCriticalSection(&cs_);
}

bool cs_lock::try_enter()
{
	return ::TryEnterCriticalSection(&cs_) ? true : false;
}

void cs_lock::enter()
{
	::EnterCriticalSection(&cs_);
}

void cs_lock::leave()
{
	::LeaveCriticalSection(&cs_);
}

auto_cs_guard::auto_cs_guard(cs_lock &lock)
	: locker_(lock)
{
	locker_.enter();
}

auto_cs_guard::auto_cs_guard(const auto_cs_guard &other)
	: locker_(other.locker_)
{
	locker_.enter();
}

auto_cs_guard::~auto_cs_guard()
{
	locker_.leave();
}

_SSWINOSL_END
_SSL_END
